﻿using e_commerce.BLL.Services.Enterprise;
using e_commerce.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using e_commerce.Filter;
using e_commerce.BLL.Models;

namespace e_commerce.Controllers
{
    
    public class OrderController : Controller
    {
        // GET: Order
        private readonly OrderService _oservice;

        public OrderController()
        {
            TempData["f-nav"] = "订单管理";
            _oservice = new OrderService();
        }

        private void SetTempData(string fnav, string snav)
        {
            TempData["f-nav"] = fnav;
            TempData["s-nav"] = snav;
        }

        [PermissionFilter(permission: Permission.Order_All)]
        public ActionResult AllOrder(int page = 1)
        {
            SetTempData("订单管理", "全部订单");
            var list = GetOrderListByState(0, page);
            return View(list);
        }

        [PermissionFilter(permission: Permission.Order_Deal)]
        public ActionResult DealOrder(int page = 1)
        {
            SetTempData("订单管理", "已处理订单");
            var list = GetOrderListByState((int)OrderState.Noreceived, page);
            return View(list);
        }


        [PermissionFilter(permission: Permission.Order_Untreated)]
        public ActionResult UntreatedOrder(int page = 1)
        {
            SetTempData("订单管理", "未处理订单");
            var list = GetOrderListByState((int)OrderState.Untreated, page);
            return View(list);
        }

        private PagedList.StaticPagedList<OrderModel> GetOrderListByState(int state, int page)
        {
            int totalCount = 0;
            int pageSize = 8;
            List<OrderModel> list = _oservice
                .GetAllOrderByPage(state, page, pageSize, ref totalCount)
                .Select(o => new OrderModel()
                {
                    OrderId = o.OrderId,
                    GoodsName = o.GoodsName,
                    Count = o.Count,
                    Price = o.Price,
                    ClientMark = o.ClientRemark,
                    State = o.State,
                    TotalAmount = o.Price * o.Count,
                }).ToList();

            var orderAsPagedList = new PagedList
                .StaticPagedList<OrderModel>(list, page, pageSize, totalCount);
            return orderAsPagedList;
        }

        [PermissionFilter(permission: Permission.Order_Deal)]
        public JsonResult AcceptOrder(string orderId)
        {
            var res = new JsonResult();
            var success = "";
            var error = "";
            try
            {
                _oservice.AcceptOrderByOrderId(orderId);
                success = "订单已接受";
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            res.Data = new { success, error };
            return res;
        }

        [PermissionFilter(permission: Permission.Order_Recycle)]
        public ActionResult OrderRecycle(int page = 1)
        {
            SetTempData("订单管理", "订单回收站");
            int pageSize = 8;
            int total = 0;
            List<OrderModel> list = _oservice.GetRecycleOrders(page, pageSize, ref total)
                .Select(o=> new OrderModel()
                {
                    OrderId = o.OrderId,
                    GoodsName = o.GoodsName,
                    Count = o.Count,
                    Price = o.Price,
                    ClientMark = o.ClientRemark,
                }).ToList();
            var orderAsPagedList = new PagedList
                .StaticPagedList<OrderModel>(list, page, pageSize, total);
            return View(orderAsPagedList);
        }

        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="orderId">订单id</param>
        /// <returns></returns>
        public ActionResult CancelOrder(string orderId)
        {
            return null;
        }

        /// <summary>
        /// 备注订单
        /// </summary>
        /// <param name="orderId">订单id</param>
        /// <param name="remark">备注</param>
        /// <returns></returns>
        public JsonResult RemarkOrder(string orderId,string remark)
        {
            return null;
        }

        [HttpPost]
        public ActionResult GetNewOrder()
        {
            TempData["url"] = "/Order/AllOrder";
            TempData["icon"] = "/Image/icon_neworder.png";
            TempData["name"] = "订单";
            TempData["title"] = "最新订单";
            var orders= _oservice.GetNewOrders(5).Select(o=>new AbbreviationsModel()
            {
                CreateTime = o.CreateTime.ToString(),
                Name = o.GoodsName,
            });
            return PartialView("~/Views/Admin/ViewComponents/NewTips.cshtml", orders);
        }
    }
}